<?php
include("../includes/DBFunctions.php");
include("../includes/getCredentials.php");

function addEnvironmentEventFunctor( $name, $prec, $heat, $sun ) {
	$gc = new GetCredentials;
	$uid = $gc->getUserID();
	$db = new DBFunctions;
	$db->connectDB();
	$result = $db->query("call addEnvironmentEvent( '".$name."', ".$prec.", ".$heat.", ".$sun.", ".$uid.", @a )" );
	$errors = $db->query("SELECT @a");
	while( $line = mysqli_fetch_array( $errors, MYSQL_ASSOC ) ) {
		$error = $line['@a'];
	}
	if( !$result || $error != 0 ) {
		die("alert('Failed stored procedure call (addEnvironmentEvent ErrorCode:".$error.")');");
	}
}

if( !isset($_POST['eventName']) || $_POST['eventName'] == '') {
	die("alert('No event name was set. Please try again.')");
}
$name = $_POST['eventName'];
if( !isset($_POST['eventPrec']) || $_POST['eventPrec'] == '') {
	die("alert('No annual rainfall was set. Please try again.')");
}
$prec = $_POST['eventPrec'];
if( !isset($_POST['eventHeat']) || $_POST['eventHeat'] == '') {
	die("alert('No daily temp was set. Please try again.')");
}
$heat = $_POST['eventHeat'];
if( !isset($_POST['eventSun']) || $_POST['eventSun'] == '') {
	die("alert('No average sunlight was set. Please try again.')");
}
$sun = $_POST['eventSun'];

addEnvironmentEventFunctor( $name, $prec, $heat, $sun );
?>